/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package network;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import weibo4j.model.WeiboException;

/**
 *
 * @author Administrator
 */
public class DataPreparation {

    public Data data;
    public Map<String, Double> S = new HashMap<String, Double>();
    public Map<String, Double> jaccard = new HashMap<String, Double>();
    public Map<String, Integer> Kout = new HashMap<String, Integer>();
    public Map<String, Integer> Kin = new HashMap<String, Integer>();
    public Map<String, String> LinkIn = new HashMap<String, String>();
    public Map<String, String> LinkOut = new HashMap<String, String>();
    public String path;
    public Map<String, Double> influence = new HashMap<String, Double>();

    public DataPreparation() throws FileNotFoundException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        String folder = Config.folderName + "\\";
        String uid = Config.uid;
        path = "F:\\Sina\\weibo4j-oauth2-beta\\src\\network\\" + folder;
        data = new Data(path, uid);
        cKout(path);
        cLinksIn(path, uid);
        cLinksOut(path, uid);
        cS();
    }

    private void cS() {
        for (String s : data.nodes) {
            S.put(s, 1.0);
        }
        S.put("0", 0.0);
    }

    private void cLinksOut(String filename, String uid) throws FileNotFoundException, IOException {
        String link0 = "";
        for (String s : data.nodes) {
            LinkOut.put(s, "0 " + uid);
            if (!s.equals("0")) {
                link0 += (s + " ");
            }
        }
        LinkOut.put("0", link0);
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename + "outNet.txt")));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename + "outNetname.txt")));
        String line = reader.readLine();
        String start = "";
        String links = "";
        String a[];
        while (line != null) {
            if (!line.isEmpty()) {
                a = line.split(" ");
                writer.write("\"" + data.username.get(a[0]) + "\"->\"" + data.username.get(a[1]) + "\",");
                if (!start.equals(a[0])) {
                    if (data.nodes.contains(a[1])) {
                        LinkOut.put(start, links);
                        links = "0 " + a[1];
                    }
                } else {
                    if (data.nodes.contains(a[1])) {
                        links += (" " + a[1]);
                    }
                }
                line = reader.readLine();
                start = a[0];
            } else {
                line = reader.readLine();
                continue;
            }
        }
        LinkOut.put(start, links);
        reader.close();
        writer.close();
    }

    private void cLinksIn(String filename, String uid) throws FileNotFoundException, IOException {
        String link0 = "";
        for (String s : data.nodes) {
            LinkIn.put(s, "0 " + uid);
            if (!s.equals("0")) {
                link0 += (s + " ");
            }
        }
        LinkIn.put("0", link0);
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename + "inNet.txt")));
        String line = reader.readLine();
        String start = "";
        String links = "";
        String a[];
        while (line != null) {
            if (!line.isEmpty()) {
                a = line.split(" ");
                if (!start.equals(a[0])) {
                    if (data.nodes.contains(a[1])) {
                        LinkIn.put(start, links);
                        Kin.put(a[0], 1);
                        links = "0 " + a[1];
                    }
                } else {
                    if (data.nodes.contains(a[1])) {
                        links += (" " + a[1]);
                        int newin = Kin.get(a[0]) + 1;
                        Kin.put(a[0], newin);
                    }
                }
                line = reader.readLine();
                start = a[0];
            } else {
                line = reader.readLine();
                continue;
            }
        }
        LinkIn.put(start, links);
        reader.close();
    }

    private void cKout(String filename) throws IOException {
        for (String s : data.nodes) {
            Kout.put(s, 1);
        }
        Kout.put("0", data.nodes.size() - 1);
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename + "outNet.txt")));
        String line = reader.readLine();
        String start = "";
        String a[];
        int kout = 1;
        int countwashoutlinks = 0;
        int count = 0;
        while (line != null) {
            if (!line.isEmpty()) {
                a = line.split(" ");
                if (!start.equals(a[0])) {
                    if (data.nodes.contains(a[1])) {
                        count += kout;
                        kout = 2;
                        Kout.put(a[0], kout);
                    } else {
                        if (data.washOutIDs.contains(a[1])) {
                            countwashoutlinks += 1;
                        }
                    }
                } else {
                    if (data.nodes.contains(a[1])) {
                        kout += 1;
                        Kout.put(a[0], kout);
                    } else {
                        if (data.washOutIDs.contains(a[1])) {
                            countwashoutlinks += 1;
                        }
                    }
                }
                start = a[0];
                line = reader.readLine();
            } else {
                line = reader.readLine();
                continue;
            }
        }
        count += kout;
        reader.close();
        for (String s : data.washOutIDs) {
            if (Kout.containsKey(s)) {
                countwashoutlinks += Kout.get(s);
                Kout.remove(s);
            }
        }
        //System.out.println(countwashoutlinks);
        //System.out.println(count);

    }
    
    
}
